﻿using System;
using System.ServiceModel;

using WSU.Cheka.Data.Contracts;

namespace WSU.Cheka.Data.Client
{
    sealed class LessonClientProxy : ClientBase<ILessonManager>, ILessonManager
    {
        #region "constructor"

        public LessonClientProxy(string endpntCfgName)
            : base(endpntCfgName)
        { }

        #endregion

        #region "implement interface"
        public void Save(Lesson lesson)
        {
            base.Channel.Save(lesson);
        }

        public Lesson Load(string name)
        {
            return base.Channel.Load(name);
        }

        public Lesson[] GetAll()
        {
            return base.Channel.GetAll();
        }
        #endregion
    }

    static class Program
    {
        static void Print(this Lesson lesson)
        {
            Console.WriteLine("\n------------- Lesson: <{0}> {1}", lesson.Id, lesson.Name);
            foreach (Student stud in lesson.Students)
                Console.WriteLine("Student[{0}]: {1}", stud.Id, stud.Name);
        }

        static void Main(string[] args)
        {
            using (LessonClientProxy proxy = new LessonClientProxy("LessonClient"))
            {
                Lesson pyLesson = new Lesson
                {
                    Id = 1,
                    Name = "Python Programming",
                    Students = new Student[]
                    {
                        new Student{Id = 1,Name = "Tom"},
                        new Student{Id = 2,Name = "Mary"}
                    }
                };
                Lesson distLesson = new Lesson
                {
                    Id = 2,
                    Name = "Distributed System",
                    Students = new Student[]
                    {
                        new Student{Id = 3,Name = "Bob"},
                        new Student{Id = 4,Name = "Alice"},
                        new Student{Id = 5,Name = "Dave"}
                    }
                };

                // ------------------------------------- saving
                proxy.Save(pyLesson);
                proxy.Save(distLesson);
                Console.WriteLine("******************************* Saving succeeds");

                // ------------------------------------- loading
                Lesson loadedLesson = proxy.Load("Python Programming");
                loadedLesson.Print();

                loadedLesson = proxy.Load("Distributed System");
                loadedLesson.Print();
                Console.WriteLine("******************************* loading succeeds");

                // ------------------------------------- getting all
                Lesson[] allLessons = proxy.GetAll();
                foreach (Lesson lesson in allLessons)
                    lesson.Print();
                Console.WriteLine("******************************* getting all succeeds");
            }
        }
    }
}
